<?php
require_once('config/init.php');
require_once('check.php');


//准备一些参数
$page = isset($_GET['page']) ? trim($_GET['page']) : 1;  //当前页码
$limit = 2; //每页显示多少

//查询员工有多少条数据
$sql = "SELECT COUNT(*) AS c FROM {$pre_}person";
$count = find($sql);
$count = $count['c'];

//总页数
$pages = ceil($count / $limit);

//准备html分页结构
$html = "<div class='pagination'><ul>";

if($page <= 1)
{
    $html .= "<li><a class='active'>首页</a></li>";
    $html .= "<li><a class='active'>上一页</a></li>";
}else
{
    $cur = $page - 1; //上一页的页码
    $html .= "<li><a href='PersonList.php'>首页</a></li>";
    $html .= "<li><a href='PersonList.php?page=$cur'>上一页</a></li>";
}

//页码数
for($i = 1; $i <= $pages; $i++)
{
    if($i == $page)  //判断是否为当前页
    {
        $html .= "<li><a class='active'>$i</a></li>";
    }else
    {
        $html .= "<li><a href='PersonList.php?page=$i'>$i</a></li>";
    }
}

if($page >= $pages)
{
    $html .= "<li><a class='active'>下一页</a></li>";
    $html .= "<li><a class='active'>尾页</a></li>";
}else
{
    $next = $page + 1; //下一页的页码
    $html .= "<li><a href='PersonList.php?page=$next'>下一页</a></li>";
    $html .= "<li><a href='PersonList.php?page=$pages'>尾页</a></li>";
}

$html .= "</ul></div>";

// echo $html;
// exit;


//查询员工列表的数据
$start = ($page - 1)*$limit;
$sql = "SELECT person.*,dep.name AS depname, job.name AS jobname FROM {$pre_}person AS person LEFT JOIN {$pre_}department AS dep ON person.depid = dep.id LEFT JOIN {$pre_}job AS job ON person.jobid = job.id ORDER BY id ASC LIMIT $start,$limit";
// echo $sql;
// exit;

$list = all($sql);

?>
<!DOCTYPE html>
<html lang="en">
    <head>
        <?php require_once('meta.php');?>
        <style>
            .pagination ul li .active{
                background:#ccc;
            }
        </style>
    </head>

    <body>
        <?php require_once('header.php');?>

        <?php require_once('menu.php');?>

        <div class="content">
            <div class="header">
                <h1 class="page-title">员工管理</h1>
            </div>
            <ul class="breadcrumb">
                <li><a href="index.php">Home</a> <span class="divider">/</span></li>
                <li class="active">Index</li>
            </ul>

            <div class="container-fluid">
                <div class="row-fluid">
                    <div class="btn-toolbar">
                        <a class="btn btn-primary" href="PersonAdd.php"><i class="icon-plus"></i> 添加员工</a>
                    </div>
                    <div class="well">
                        <table class="table">
                            <thead>
                                <tr>
                                    <th>ID</th>
                                    <th>名称</th>
                                    <th>手机号码</th>
                                    <th>入职时间</th>
                                    <th>所在部门</th>
                                    <th>所在职位</th>
                                    <th style="width: 50px;">操作</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php foreach($list as $item){?>
                                <tr>
                                    <td><?php echo $item['id'];?></td>
                                    <td><?php echo $item['nickname'];?></td>
                                    <td><?php echo $item['mobile'];?></td>
                                    <td><?php echo date("Y-m-d",$item['createtime']);?></td>
                                    <td><?php echo empty($item['depname']) ? "暂无部门" : $item['depname'];?></td>
                                    <td><?php echo empty($item['jobname']) ? "暂无职位" : $item['jobname'];?></td>
                                    <td>
                                        <a href="add.html"><i class="icon-pencil"></i></a>
                                        <a href="#myModal" role="button" data-toggle="modal"><i class="icon-remove"></i></a>
                                    </td>
                                </tr>
                                <?php }?>
                            </tbody>
                        </table>
                    </div>
                    
                    <?php echo $html; ?>
                    <!-- <div class="pagination">
                        <ul>
                            <li><a href="#">Prev</a></li>
                            <li><a href="#">1</a></li>
                            <li><a href="#">2</a></li>
                            <li><a href="#">3</a></li>
                            <li><a href="#">4</a></li>
                            <li><a href="#">Next</a></li>
                        </ul>
                    </div> -->

                    <div class="modal small hide fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                        <div class="modal-header">
                            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                            <h3 id="myModalLabel">Delete Confirmation</h3>
                        </div>
                        <div class="modal-body">
                            <p class="error-text"><i class="icon-warning-sign modal-icon"></i>Are you sure you want to delete the user?</p>
                        </div>
                        <div class="modal-footer">
                            <button class="btn" data-dismiss="modal" aria-hidden="true">Cancel</button>
                            <button class="btn btn-danger" data-dismiss="modal">Delete</button>
                        </div>
                    </div>

                    <footer>
                        <hr>
                        <p>&copy; 2017 <a href="#" target="_blank">copyright</a></p>
                    </footer> 
                </div>
            </div>
        </div>
    </body>
</html>